package com.csu.express.controller.app;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csu.common.exception.WebException;
import com.csu.common.vo.ResultVo;
import com.csu.express.constants.ErrorCode;
import com.csu.express.pojo.OrderPojo;
import com.csu.express.service.OrderService;
import com.csu.express.vo.AssessVo;
import com.csu.express.vo.OrderRequestVo;
import com.csu.express.vo.OrderResponseVo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：express
 * @类名称：OrderController
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年10月23日 上午9:25:21
 * @company:
 */
@Controller
@RequestMapping("/order")
@Api(value = "order", description = "订单服务接口")
public class OrderController
{

    @Autowired
    private OrderService orderService;

    @ResponseBody
    @ApiOperation(value = "计算订单参考价格", notes = "给出订单的参考价格，不包含等待时间")
    @RequestMapping(value = "assessOrder", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<Double> assessOrder(HttpServletRequest request, @RequestBody AssessVo assessVo) throws WebException
    {
        ResultVo<Double> result = new ResultVo<Double>();
        Double price = orderService.assessOrder(assessVo);
        result.setData(price);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "添加订单", notes = "用户下单接口")
    @RequestMapping(value = "addOrder", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<OrderPojo> addOrder(HttpServletRequest request, @RequestBody OrderRequestVo orderRequest)
            throws WebException
    {
        ResultVo<OrderPojo> result = new ResultVo<OrderPojo>();
        OrderPojo order = orderService.addOrder(orderRequest);
        result.setData(order);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "取消订单", notes = "用户取消下单")
    @RequestMapping(value = "cancelOrder", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> cancelOrder(HttpServletRequest request,
            @ApiParam(value = "订单流水号") @RequestParam(value = "serialNumber", required = true) String serialNumber,
            @ApiParam(value = "客户id") @RequestParam(value = "customerId", required = true) Integer customerId)
            throws WebException
    {
        ResultVo<Integer> result = new ResultVo<Integer>();
        int num = orderService.cancelOrder(serialNumber, customerId);
        result.setData(num);
        if(num!=1){
            result.setErrorCode(ErrorCode.OrderNotExitCode);
            result.setErrorInfo(ErrorCode.OrderNotExitMessage);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查询订单详细信息", notes = "查询订单相关的信息")
    @RequestMapping(value = "getOrderInfo/{serialNumber}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<OrderResponseVo> getOrderInfo(HttpServletRequest request,
            @ApiParam(value = "订单流水号") @PathVariable(value = "serialNumber") String serialNumber) throws WebException
    {
        ResultVo<OrderResponseVo> result = new ResultVo<OrderResponseVo>();
        OrderResponseVo orderRep = orderService.getOrderInfoBySn(serialNumber);
        result.setData(orderRep);
        return result;
    }

}
